package srzy.common

import java.net.InetAddress
import java.lang.reflect.Field
import java.io.File

object Config extends Logging {

  private val fileType = "YAML"

  def getValue(seq: String*): String = {
    (fileType match {
      case "YAML" => ConfigYAML.getValue(seq: _*)
      case "XML" => ConfigXML.getValue(seq: _*)
    }) match {
      case Some(v) => v
      case None => throw new Exception("Config:" + seq.mkString(",") + " is not assigned")
    }
  }

  def getItems(x: String): Option[Map[String, String]] = {
    fileType match {
      case "YAML" => ConfigYAML.getItems(x)
      case "XML" => ConfigXML.getItems(x)
    }
  }

  def envMap = {
    (
      Config.getClass.getDeclaredFields.map(_.getName).toList.filter((t: String) => {
        """[^\w]""".r findFirstIn t match {
          case None => true
          case _ => false
        }
      }).filter(x => x matches "[A-Z0-9_]*")
      .map(x => (x, ((x: Field) => {
        x.setAccessible(true)
        x.get(Config).toString()
      })(Config.getClass.getDeclaredField(x))))).toMap
  }

}

trait Config {
  val typeToCdrMap = CdrFieldParser.parse(Config.getValue("root", "config", "TypeToCdrInt"))
  //  val cdrDirMap = CdrLogParser.parse(Config.getValue("root", "config", "CdrDir"))
  val cdrRootPaths = Config.getValue("root", "config", "RootPaths")
  val cdrPaths = getDirectory(new File(cdrRootPaths)).map(e => ("1" + e._2, e._1))

  //    "DATA:/sm/hp_sm/acgzone/cdr/cur/Data/NORMAL_FILE_OUTPUT;Dsl:/sm/hp_sm/acgzone/cdr/cur/Data/NORMAL_FILE_OUTPUT;toHBSms:/sm/hp_sm/acgzone/cdr/toHB/Sms/NORMAL_FILE_OUTPUT"

}